#include "StdAfx.h"
#include "ID.h"

CID::CID(void)
{
	memset(this->m_rgID,0,sizeof(DWORD64)*4);
}

CID::CID(DWORD64 n0,DWORD64 n1,DWORD64 n2,DWORD64 n3)
{
	this->m_rgID[0]=n0;
	this->m_rgID[1]=n1;
	this->m_rgID[2]=n2;
	this->m_rgID[3]=n3;
}

void CID::SetValue(DWORD64 n0,DWORD64 n1,DWORD64 n2,DWORD64 n3)
{
	this->m_rgID[0]=n0;
	this->m_rgID[1]=n1;
	this->m_rgID[2]=n2;
	this->m_rgID[3]=n3;
}
CID::CID(const string tmp)
{
	string strId(tmp);
	int nPos=(int)strId.find_last_of(';');
	if(string::npos==nPos)
	{
		memset(this->m_rgID,0,sizeof(DWORD64)*4);
		return ;
	}
	else
	{
		strId.erase(nPos);
	}
	nPos = (int)strId.find_last_of(' ');
	while(nPos == strId.length()-1)
	{
		strId.erase(nPos);
	}
	unsigned int sgNumber[SIZE*2];
	ZeroMemory(sgNumber,IDSIZE);
	for(int i=0;i<7;i++)
	{
		nPos = (int)strId.find_last_of(' ');
		sgNumber[i]=(int)_atoi64(strId.substr(nPos).c_str());
		strId.erase(nPos);

	}
	sgNumber[7]=(int)_atoi64(strId.c_str());
	memcpy_s(m_rgID,IDSIZE,sgNumber,IDSIZE);
};
CID::~CID(void)
{
}
CID::CID(const CID & tmp)
{
	memcpy(this->m_rgID,tmp.m_rgID,sizeof(DWORD64)*4);
}
CID & CID::operator = (const CID & tmp)
{
	memcpy(this->m_rgID,tmp.m_rgID,sizeof(DWORD64)*SIZE);
	return *this;
}
CID & CID::operator = (const string tmp)
{
	string strId(tmp);
	int nPos=(int)strId.find_last_of(';');
	if(string::npos==nPos)
	{
		return * this;
	}
	else
	{
		strId.erase(nPos);
	}
	nPos = (int)strId.find_last_of(' ');
	while(nPos == strId.length()-1)
	{
		strId.erase(nPos);
	}
	unsigned int sgNumber[SIZE*2];
	ZeroMemory(sgNumber,IDSIZE);
	for(int i=0;i<7;i++)
	{
		nPos = (int)strId.find_last_of(' ');
		sgNumber[i]=(int)_atoi64(strId.substr(nPos).c_str());
		strId.erase(nPos);

	}
	sgNumber[7]=(int)_atoi64(strId.c_str());
	memcpy_s(m_rgID,IDSIZE,sgNumber,IDSIZE);
	return * this;
};
bool operator==(const CID & tmp1,const CID & tmp2)
{
	return memcmp(tmp1.m_rgID,tmp2.m_rgID,sizeof(DWORD64)*SIZE)==0;
}
bool operator>(const CID & tmp1,const CID & tmp2)
{ 
	for(int i=SIZE-1;i>=0;i--)
	{
		if(tmp1.m_rgID[i]==tmp2.m_rgID[i])
		{
			continue;
		}
		else
		{
			return tmp1.m_rgID[i]>tmp2.m_rgID[i];
		}
	}
	return false;
}
bool operator<(const CID & tmp1,const CID & tmp2)
{
	if(tmp1==tmp2)
		return false;
	return !(tmp1>tmp2);
}
bool operator>=(const CID & tmp1,const CID & tmp2)
{
	return !(tmp1<tmp2);
}
bool operator<=(const CID & tmp1,const CID & tmp2)
{
	return !(tmp1>tmp2);
}
bool operator!=(const CID & tmp1,const CID & tmp2)
{
	return !(tmp1==tmp2);
}

istream & operator >> (istream & in,CID & cId)
{
	unsigned int sgNumber[SIZE*2];
	for(int i=0;i<8;i++)
	{
		in>>sgNumber[7-i];
	}
	char c;
	do 
	{
		in>>c;
		if(c==';')
		{
			memcpy(cId.m_rgID,sgNumber,SIZE*2);
			return in;
		}
	}while(c==' ');
	printf("Input Error!!\r\n");
	return in;
}
ifstream & operator >> (ifstream & in,CID & cId)
{

	unsigned int sgNumber[SIZE*2];
	ZeroMemory(sgNumber,IDSIZE);
	for(int i=0;i<8;i++)
	{
		in>>sgNumber[7-i];
	}
	char c;
	do 
	{
		in>>c;
		if(c==';')
		{
			memcpy_s(cId.m_rgID,IDSIZE,sgNumber,IDSIZE);
			return in;
		}
	}while(c==' ');
	return in;
}
ofstream & operator << (ofstream & out,const CID & cId)
{
	for(int i =0;i<SIZE;i++)
	{
		DWORD rgTmp[2];
		memcpy_s(rgTmp,sizeof(DWORD64),&cId.m_rgID[SIZE-1-i],sizeof(DWORD64));
		out<<rgTmp[1]<<' '<<rgTmp[0]<<' ';
	}
	
	out<<';'<<endl;
	return out;
}
ostream & operator << (ostream & out,const CID & cId)
{
	for(int i =0;i<SIZE;i++)
	{
		DWORD rgTmp[2];
		memcpy_s(rgTmp,sizeof(DWORD64),&cId.m_rgID[SIZE-1-i],sizeof(DWORD64));
		out<<hex<<rgTmp[1]<<' '<<hex<<rgTmp[0]<<dec;
		if(SIZE-1-i!=0)
		{
			out<<' ';
		}
	}
	out<<';'<<endl;
	return out;
}
